#ifdef RT_USE_OLD_CONN_WRAP

#if !defined PKG_H  && !defined (_NEW_PROTO_TP)
#define PKG_H

#include "RtCnBase.h"
#include "RtCnRlbTcp.h"

#define SEND_BUF_LEN_MAX4PKG		65536	//64K bytes 

#define SHORT_MSEC_INTERVAL1		(LONG)200000	//200ms
#define SHORT_MSEC_INTERVAL2		(LONG)500000	//500ms
#define SHORT_MSEC_INTERVAL3		(LONG)1000000	//1s

#define MAX_CONN_RESP_NO_RECV		6		//UDP case, retry for 3 times sending ConnReq

#define TV_FOR_RELEASE				(LONG)60		//1minutes

class CPkgConnClient;
class CPkgConnServer;

typedef ServerListT<CPkgConnServer>			CPkgConnServerList;

typedef CConnAcceptorSinkT<CPkgConnServer>	CPkgConnAcceptorSink;
typedef CConnAcceptorT<CPkgConnServer>		CPkgConnAcceptor;

typedef CConnConnectorT<CPkgConnClient>		CPkgConnConnector;

class /*RT_API_EXPORT*/ CPkgConn : public CRtCnConn
{
public:
	virtual RtResult SetOption(
		DWORD aCommand, 
		LPVOID aArg);

	virtual RtResult GetOption(
		DWORD aCommand, 
		LPVOID aArg);

	virtual RtResult Disconnect(RtResult aReason);
	
	virtual RtResult SendData(
						  CRtMessageBlock &aData, 
						  CRtTransportParameter *aPara);
	virtual RtResult SendDisconn(RtResult aReason);
public:
	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId) = 0;

	virtual void OnTimer(
		CRtTimerWrapper* aId) = 0;

	virtual void OnSend(
		IRtTransport *aTrptId,
		CRtTransportParameter *aPara = NULL);
protected:
	//Internal functions
	virtual RtResult SendKeepAlive();
	virtual RtResult SendDataFromSendBuf();
	void SendRtt(DWORD inTimestamp);
	void Reset();
public:
	CPkgConn(DWORD dwMaxSendBufLen);
	virtual ~CPkgConn();
protected:
	BOOL m_bPkgNeedBuf;

	BOOL m_needOnSend;

	//local Data block for Send, when PKG-TCP with no SendBuf
	CRtMessageBlock *m_pmbLocSendData;	

	int m_nUngetDataCnt;//Counter for times haven't received data

	BOOL m_bConnIndicate2Upper;// If has Connect Indicate to Upper layer already
};

class /*RT_API_EXPORT*/ CPkgConnClient : public CPkgConn
{
public:
	// interface IRtAcceptorConnectorSink
	virtual void OnConnectIndication(
		RtResult aReason,
		IRtTransport *aTrpt,
		IRtAcceptorConnectorId *aRequestId);

	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId);
	
	virtual void OnTimer(CRtTimerWrapper* aId);

public:
	void SetConnConnector(CPkgConnConnector* pConnConnector);

	//Cancel connect call, stop the handshake if connection hasn't made
	void CancelHandShake();
	void ScheduleConnectTimer(const CRtTimeValue& aTimeout) {}

protected:
	//Internal functions	
	virtual void OnRecvConnResp();
	virtual void OnRecvDisconn();
	virtual void OnRecvRtt(DWORD inTimestamp);
public:
	CPkgConnClient();
	virtual ~CPkgConnClient();
protected:
	int m_nConnReqCnt;	//Counter for ConnReq PDU send
	CPkgConnConnector* m_pConnConnector;			//Only Client use
	BOOL m_bConnRespRecved;

	BOOL m_bConnectOnceMade;
	BOOL m_bHandShakeCancelled;

	DWORD m_lastRtt;
};

class /*RT_API_EXPORT*/ CPkgConnServer : public CPkgConn
{
public:
	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId);
	
	virtual void OnTimer(CRtTimerWrapper* aId);

	// interface IRtAcceptorConnectorSink
	virtual void OnConnectIndication(
		RtResult aReason,
		IRtTransport *aTrpt,
		IRtAcceptorConnectorId *aRequestId);
public:
	//Unconnected time stamp
	CRtTimeValue GetDisconnTimestamp();
public:
	CPkgConnServer();
	virtual ~CPkgConnServer();

	//In order to keep one instance of Server List.
	void SetServerList(CPkgConnServerList *pSvrList);
	void SetConnAcceptor(CPkgConnAcceptor* pConnAcceptor);
protected:
	virtual void OnRecvConnReq();
	virtual void OnRecvRtt(DWORD inTimestamp);
	void Reset();
	
protected:
	CPkgConnServerList* m_pSvrList;
	CRtAutoPtr<CPkgConnAcceptor> m_pConnAcceptor;

	//CRtTimeValue m_disconn_timestamp;
//	BOOL m_bConnReqRecved;

	CRtTimerWrapper m_ReleaseTimer;
};

#endif	//PKG_H

#endif // RT_USE_OLD_CONN_WRAP
